###########################
#Replication Code for:
# TITLE HERE

#Date: April 7, 2022
#Version 1
#Nazita Lajevardi
#Kassra Oskooii
#Hannah Walker
###########################

rm(list=ls())

################
#Load Packages #
################
library(tidyverse)
library(haven)
library(margins)
library(stargazer)
library(descr)
library(survey)



######################
#Load All Data sets #
######################

######
#SSI #
######
ssi<-read_csv("ssi_data.csv")

ssi_final<-ssi%>% 
  dplyr::select(ban, patrol, socialmedia, localTV, cableTV, radio, 
                newspaper_online_print, democrat, republican, independent, 
                female, white, age, income, educ) %>% data.frame()

#Subset by PID
ssi_dem<-ssi%>% 
  filter(democrat==1)  %>% 
  dplyr::select(ban, patrol, socialmedia, localTV, cableTV, radio, 
                newspaper_online_print, female, white, age, income, educ) %>% 
                data.frame() 

ssi_rep<-ssi%>% 
  filter(republican==1)  %>% 
  dplyr::select(ban, patrol, socialmedia, localTV, cableTV, radio, 
                newspaper_online_print, female, white, age, income, educ) %>% 
                data.frame() 

###################
#Lucid March Data #
###################
lucid_march<-read_csv("lucid_march_data.csv")

lucid_march_final<-lucid_march%>% 
  dplyr::select(ban, patrol, epa, assisted_suicide, socialmedia, info_Twitter, 
                info_Facebook, broadcast_tv, info_CNN, info_FOX, info_MSNBC, 
                info_radio, info_localnewspaper, info_NYT, democrat, republican, 
                independent, ideology_7pt, female, white, age, income, educ, 
                fav_MuslimAmericans) %>% 
                data.frame()

#Subset by PID
lucid_march_dem<-lucid_march%>% 
  filter(democrat==1)  %>% 
  dplyr::select(ban, patrol, socialmedia, info_Twitter, info_Facebook, 
                broadcast_tv, info_CNN, info_FOX, info_MSNBC,info_radio, 
                info_localnewspaper, info_NYT, ideology_7pt, female, 
                white, age, income, educ, fav_MuslimAmericans) %>% 
                data.frame() 

lucid_march_rep<-lucid_march%>% 
  filter(republican==1)  %>% 
  dplyr::select(ban, patrol, socialmedia, info_Twitter, info_Facebook, 
                broadcast_tv, info_CNN, info_FOX, info_MSNBC, info_radio, 
                info_localnewspaper, info_NYT, ideology_7pt, female, white, 
                age, income, educ, fav_MuslimAmericans) %>% 
                data.frame() 

###################
#Lucid June Data #
###################
lucid_june<-read_csv("lucid_june_data.csv")

lucid_june_final<-lucid_june%>% 
  dplyr::select(ban, patrol, restrict_mosques, noweapon_muslims, socialmedia, 
                info_Twitter, info_Facebook, broadcast_tv, info_CNN, info_FOX, 
                info_MSNBC, info_radio_npr, info_radio_hannity, 
                info_localnewspaper, info_NYT, info_wsj, democrat, republican, 
                independent, ideology_7pt, female, white, age, income, educ, 
                fav_MuslimAmericans) %>% 
                data.frame()


lucid_june_dem<-lucid_june%>% 
  filter(democrat==1)  %>% 
  dplyr::select(ban, patrol, restrict_mosques, noweapon_muslims, socialmedia, 
                info_Twitter, info_Facebook, broadcast_tv, info_CNN, info_FOX, 
                info_MSNBC, info_radio_npr, info_radio_hannity, 
                info_localnewspaper, info_NYT, info_wsj, ideology_7pt, 
                female, white, age, income, educ, fav_MuslimAmericans) %>% 
                data.frame() 

lucid_june_rep<-lucid_june%>% 
  filter(republican==1)  %>% 
  dplyr::select(ban, patrol, restrict_mosques, noweapon_muslims, socialmedia, 
                info_Twitter, info_Facebook, broadcast_tv, info_CNN, info_FOX, 
                info_MSNBC, info_radio_npr, info_radio_hannity, 
                info_localnewspaper, info_NYT, info_wsj,ideology_7pt, 
                female, white, age, income, educ, fav_MuslimAmericans) %>% 
                data.frame() 

###################
#Nationscape Data #
###################
nation <- read_dta("2019_Nationscape_July_Dec_waves.dta")

## Create Variables
nation_final<-nation %>% mutate(socialmedia=case_when(news_sources_facebook==1~1,
                                                      news_sources_facebook==2~0),
                                info_CNN=case_when(news_sources_cnn==1~1,
                                                   news_sources_cnn==2~0),
                                info_MSNBC=case_when(news_sources_msnbc==1~1,
                                                     news_sources_msnbc==2~0),
                                info_FOX=case_when(news_sources_fox==1~1,
                                                   news_sources_fox==2~0),
                                broadcast_tv=case_when(news_sources_network==1~1,
                                                       news_sources_network==2~0),
                                info_radio_npr= case_when(news_sources_npr==1~1,
                                                          news_sources_npr==2~0),
                                info_NYT= case_when(news_sources_new_york_times==1~1,
                                                    news_sources_new_york_times==2~0),
                                info_localnewspaper = 
                                  case_when(news_sources_local_newspaper==1~1,
                                            news_sources_local_newspaper==2~0))

#Recode Variables
nation_final$ideo5<- car::recode(nation$ideo5, "1=1;; 2=2; 3=3; 4=4; 5=5; else=NA")
nation_final$cap_carbon<-car::recode(nation$cap_carbon,"1=1; 2=0; 999=.5")
nation_final$cut_taxes<-car::recode(nation$mctaxes,"1=1; 2=0; 999=.5")
nation_final$wall<-car::recode(nation$wall,"1=1; 2=0; 999=.5") #disagree to agree, not sure mid point

nation_final$female<-car::recode(nation$gender,"1=1; 2=0; else=NA")
nation_final$white<-car::recode(nation$race_ethnicity,"1=1; else=0")
nation_final$income<-car::recode(nation$household_income,"1:4=0; 5:10=1; 11:15=2; 16:19=3; 20:24=4")
nation_final$education<-car::recode(nation$education,"1:3=0; 4=1; 5:7=2; 8=3; 9:11=4")
nation_final$democrat<-car::recode(nation$pid7,"1:3=1; else=0")
nation_final$independent<-car::recode(nation$pid7,"4=1; else=0")
nation_final$republican<-car::recode(nation$pid7,"5:7=1; else=0")
nation_final$muslim_favorability<- car::recode(nation$group_favorability_muslims,"4=0; 3=1; 999=2; 2=3; 1=4")

# Muslim Ban DVs
nation_final$ban<-car::recode(nation$muslimban,"1=1; 2=0; 999=.5; 888=NA")
nation_final$ban2<-car::recode(nation$muslimban,"1=1; 2=0; else=NA")



###################################
#Descriptive Stats - Tables A1-A4 #
###################################

#A1
stargazer(
  ssi_final, 
  summary.stat = c("mean", "median", "sd", "min", "max"),
  digits = 2,
  covariate.labels=c("Ban Support", "Patrol Support", "Social Media","Broadcast TV", "Cable TV", "Radio", "Newspapers", "Democrat", "Republican",  "Independent", "Female", "White", "Age", "Income", "Education"),
  out = "descriptive_ssi.tex",
  title="2016 SSI Descriptive Statistics",
  label="desc_ssi")

#A2
stargazer(
  lucid_march_final, 
  summary.stat = c("mean", "median", "sd", "min", "max"),
  digits = 2,
  covariate.labels=c("Ban Support", "Patrol Support", "EPA", "Assisted Suicide", "Social Media","Twitter", "Facebook", "Broadcast TV", "CNN", "FOX", "MSNBC", "Radio", "Local Newspaper", "NYT", "Democrat", "Republican", "Independent", "Ideology (Lib-Con)", "Female", "White", "Age", "Income", "Education", "Muslim Favorability"),
  out = "descriptive_lucid_march.tex",
  title="2019 LUCID March Descriptive Statistics",
  label="desc_lucid_march")

#A3
stargazer(
  lucid_june_final, 
  summary.stat = c("mean", "median", "sd", "min", "max"),
  digits = 2,
  covariate.labels=c("Ban Support", "Patrol Support", "Restrict Mosques", "Weapon Ban", "Social Media","Twitter", "Facebook", "Broadcast TV", "CNN", "FOX", "MSNBC", "Radio NPR", "Radio Hannity", "Local Newspaper", "NYT", "WSJ", "Democrat", "Republican", "Independent", "Ideology (Lib-Con)", "Female", "White", "Age", "Income", "Education", "Muslim Favorability"),
  out = "descriptive_lucid_june.tex",
  title="2019 LUCID June Descriptive Statistics",
  label="desc_lucid_june")


#A4
nation_summarystat<-nation_final%>% 
  dplyr::select(ban, socialmedia, broadcast_tv, info_CNN, info_FOX, info_MSNBC, info_radio_npr, info_localnewspaper,  
                info_NYT, 
                democrat, republican,independent, ideo5, 
                female, age, white, income, education) %>%  
                drop_na() %>% 
                data.frame()

stargazer(
  nation_summarystat, 
  summary.stat = c("mean", "median", "sd", "min", "max"),
  digits = 2,
  covariate.labels=c("Ban Support", "Social Media", "Broadcast TV", "CNN", "FOX", "MSNBC", "Radio NPR", "Local Newspaper", "NYT", "Democrat", "Republican", "Independent", "Ideology (Lib-Con)", "Female", "Age", "White", "Income", "Education"),
  out = "descriptive_nationscape.tex",
  title="2019 Nationscape Descriptive Statistics",
  label="desc_nationscape")



##################
#Analysis Tables #
##################

############
# Table B1 #
############
#Predict Social Media Use 
summary(socialmedia_predict_ssi <-
          lm(socialmedia ~ female + age + white+ income+ educ+  
               republican +  independent,    
             data = ssi_final))

summary(socialmedia_predict_lucid_march <-
          lm(socialmedia ~ female + age + white+ income+ educ + republican +  
               independent+ ideology_7pt, data = lucid_march_final))

summary(socialmedia_predict_lucid_june <-
          lm(socialmedia ~female + age + white+ income+ educ + republican +  
               independent+ ideology_7pt, data = lucid_june_final))

tab_labels <- c("Female", "Age",  "White", "Income", "Education", 
                "Republican", "Independent", "Ideology (Lib-Con)")

stargazer(socialmedia_predict_ssi, socialmedia_predict_lucid_march, 
          socialmedia_predict_lucid_june,
          style="ajps", out.header=T,
          model.numbers = TRUE,
          covariate.labels = tab_labels,
          dep.var.labels =  c("Social Media"),
          out = "table_predict_socialmedia.tex", 
          label = "table_predict_socialmedia",
          font.size = "footnotesize",
          column.sep.width = "2pt",
          omit.stat = c("ser","f")
)



############
# Table B2 #
############
#Ban SSI
summary(ban_ssi1 <-
          lm(ban ~ socialmedia +  republican +  independent + female + 
               age + white+ income+ educ,
             data = ssi_final))

summary(ban_ssi2 <-
          lm(ban ~ socialmedia +  republican +  independent + female + 
               age + white+ income+ educ + localTV +cableTV + radio+ 
               newspaper_online_print,    
             data = ssi_final))

#Patrol SSI
summary(patrol_ssi1 <-
          lm(patrol ~ socialmedia +  republican +  independent + female + 
               age + white+ 
               income+ educ, data = ssi_final))

summary(patrol_ssi2 <-
          lm(patrol ~ socialmedia +  republican +  independent + female + 
               age + white+ income+ educ + localTV +cableTV + radio+ 
               newspaper_online_print,    
             data = ssi_final))


tab_labels <- c( "Social Media", "Republican", "Independent", "Female",  
                 "Age",  "White", "Income", "Education", "Broadcast TV", 
                 "Cable TV", "Radio", "Newspaper")

stargazer(ban_ssi1, ban_ssi2, patrol_ssi1, patrol_ssi2, 
          style="ajps", out.header=T,
          model.numbers = TRUE,
          covariate.labels = tab_labels,
          dep.var.labels =  c("Support Ban", "Support Patrol"),
          out = "table_ssi_main.tex", 
          label = "table_ssi_main",
          font.size = "footnotesize",
          column.sep.width = "2pt",
          omit.stat = c("ser","f")
)


################################
# SSI Predictions for Plotting #
################################

margin_ssi_ban <- as.data.frame(summary(margins(ban_ssi2,
                  variables=c("socialmedia","localTV", "cableTV",
                              "radio","newspaper_online_print"), 
                  change = "minmax"))) %>% select(c("AME","lower","upper", "factor"))

margin_ssi_patrol <- as.data.frame(summary(margins(patrol_ssi2,
                    variables=c("socialmedia","localTV", "cableTV",
                                "radio","newspaper_online_print"), 
                    change = "minmax"))) %>% select(c("AME","lower","upper", "factor"))


############
# Table B3 #
############

#Ban Lucid March
summary(ban_lucid_march_1 <-
          lm(ban ~ socialmedia + republican +  independent + ideology_7pt+ 
               female + age + white+ income+ educ + fav_MuslimAmericans, 
             data = lucid_march_final))

summary(ban_lucid_march_2 <-
          lm(ban ~ socialmedia + republican +  independent + ideology_7pt+ 
               female + age + white+ income+ educ + fav_MuslimAmericans+ 
               broadcast_tv+ info_CNN + info_FOX + info_MSNBC + info_radio+ 
               info_localnewspaper+ info_NYT, data = lucid_march_final))

#Patrol Lucid March
summary(patrol_lucid_march_1 <-
          lm(patrol ~ socialmedia + republican +  independent + ideology_7pt+ 
               female + age + white+ income+ educ + fav_MuslimAmericans, 
             data = lucid_march_final))

summary(patrol_lucid_march_2 <-
          lm(patrol ~ socialmedia + republican +  independent + ideology_7pt+ 
               female + age + white+ income+ educ + fav_MuslimAmericans+ 
               broadcast_tv+ info_CNN + info_FOX + info_MSNBC + info_radio+ 
               info_localnewspaper+ info_NYT, data = lucid_march_final))


tab_labels <- c( "Social Media", "Republican", "Independent", 
                 "Ideology (Lib-Con)", "Female", "Age",  "White", 
                 "Income", "Education", "Muslim Favorability", 
                 "Broadcast TV", "CNN", "FOX", "MSNBC", "Radio", 
                 "Local Newspaper", "NYT")

stargazer(ban_lucid_march_1, ban_lucid_march_2, patrol_lucid_march_1, 
          patrol_lucid_march_2, style="ajps", out.header=T,
          model.numbers = TRUE,
          covariate.labels = tab_labels,
          dep.var.labels =  c("Support Ban", "Support Patrol"),
          out = "table_lucid_march_main.tex", 
          label = "table_lucid_march_main",
          font.size = "footnotesize",
          column.sep.width = "2pt",
          omit.stat = c("ser","f")
)



########################################
# March Lucid Predictions for Plotting #
########################################

margin_marchlucid_ban <- as.data.frame(summary(margins(ban_lucid_march_2,
                         variables=c("socialmedia","broadcast_tv", "info_CNN", 
                                     "info_FOX", "info_MSNBC", "info_radio", 
                                     "info_localnewspaper", "info_NYT"), 
                         change = "minmax"))) %>% select(c("AME","lower","upper", "factor"))

margin_marchlucid_patrol <- as.data.frame(summary(margins(patrol_lucid_march_2,
                            variables=c("socialmedia", "broadcast_tv", "info_CNN", 
                                        "info_FOX", "info_MSNBC", "info_radio", 
                                        "info_localnewspaper", "info_NYT"), 
                            change = "minmax"))) %>% select(c("AME","lower","upper", "factor"))



############
# Table B4 #
############

#Ban Lucid June
summary(ban_lucid_june_1 <-
          lm(ban ~ socialmedia + republican +  independent + ideology_7pt+ 
               female + age + white+ income+ educ + fav_MuslimAmericans, 
             data = lucid_june_final))

summary(ban_lucid_june_2 <-
          lm(ban ~ socialmedia + republican +  independent + ideology_7pt+ 
               female + age + white+ income+ educ + fav_MuslimAmericans+ 
               broadcast_tv+ info_CNN + info_FOX + info_MSNBC + info_radio_npr+
               info_radio_hannity + info_localnewspaper+ info_NYT + info_wsj, 
             data = lucid_june_final))


#Patrol Lucid June
summary(patrol_lucid_june_1 <-
          lm(patrol ~ socialmedia + republican +  independent + ideology_7pt+ 
               female + age + white+ income+ educ + fav_MuslimAmericans, 
             data = lucid_june_final))

summary(patrol_lucid_june_2 <-
          lm(patrol ~ socialmedia + republican +  independent + ideology_7pt+ 
               female + age + white+ income+ educ + fav_MuslimAmericans+ 
               broadcast_tv+ info_CNN + info_FOX + info_MSNBC + info_radio_npr+ 
               info_radio_hannity + info_localnewspaper+ info_NYT+info_wsj, 
             data = lucid_june_final))


#Restrict Mosques Lucid June
summary(mosque_lucid_june_1 <-
          lm(restrict_mosques ~ socialmedia + republican +  independent + 
               ideology_7pt+ female + age + white+ income+ educ + 
               fav_MuslimAmericans, 
             data = lucid_june_final))

summary(mosque_lucid_june_2 <-
          lm(restrict_mosques ~ socialmedia + republican +  independent + 
               ideology_7pt+ female + age + white+ income+ educ + 
               fav_MuslimAmericans+ broadcast_tv+ info_CNN + info_FOX + 
               info_MSNBC + info_radio_npr+ info_radio_hannity + 
               info_localnewspaper+ info_NYT+info_wsj, 
             data = lucid_june_final))


#Weapon Ban Lucid June
summary(weapon_lucid_june_1 <-
          lm(noweapon_muslims ~ socialmedia + republican +  independent + 
               ideology_7pt+ female + age + white+ income+ educ + 
               fav_MuslimAmericans, data = lucid_june_final))

summary(weapon_lucid_june_2 <-
          lm(noweapon_muslims ~ socialmedia + republican +  independent + 
               ideology_7pt+ female + age + white+ income+ educ + 
               fav_MuslimAmericans+ broadcast_tv+ info_CNN + info_FOX + 
               info_MSNBC + info_radio_npr+ info_radio_hannity + 
               info_localnewspaper+ info_NYT+info_wsj, 
             data = lucid_june_final))


tab_labels <- c( "Social Media", "Republican", "Independent", 
                 "Ideology (Lib-Con)", "Female",  "Age",  "White", "Income", 
                 "Education", "Muslim Favorability", "Broadcast TV", "CNN", 
                 "FOX", "MSNBC", "Radio NPR", "Radio Hannity", 
                 "Local Newspaper", "NYT", "WSJ")

stargazer(ban_lucid_june_1, ban_lucid_june_2, patrol_lucid_june_1, 
          patrol_lucid_june_2, mosque_lucid_june_1, mosque_lucid_june_2,
          weapon_lucid_june_1, weapon_lucid_june_2, 
          style="ajps", out.header=T,
          model.numbers = TRUE,
          covariate.labels = tab_labels,
          dep.var.labels =  c("Support Ban", "Support Patrol", 
                              "Restrict Mosques", "Weapon Ban"),
          out = "table_lucid_june_main.tex", 
          label = "table_lucid_june_main",
          font.size = "footnotesize",
          column.sep.width = "2pt",
          omit.stat = c("ser","f")
)


########################################
# June Lucid Predictions for Plotting #
########################################

margin_junelucid_ban <- as.data.frame(summary(margins(ban_lucid_june_2,
                        variables=c("socialmedia",  "broadcast_tv", "info_CNN", 
                                    "info_FOX", "info_MSNBC", "info_radio_npr", 
                                    "info_radio_hannity", "info_localnewspaper", 
                                    "info_NYT","info_wsj"), change = "minmax"))) %>% 
                        select(c("AME","lower","upper", "factor"))

margin_junelucid_patrol <- as.data.frame(summary(margins(patrol_lucid_june_2,
                          variables=c("socialmedia",  "broadcast_tv", "info_CNN",
                                      "info_FOX", "info_MSNBC", "info_radio_npr", 
                                      "info_radio_hannity", "info_localnewspaper", 
                                      "info_NYT","info_wsj"), change = "minmax"))) %>% 
                         select(c("AME","lower","upper", "factor"))

margin_junelucid_mosque <- as.data.frame(summary(margins(mosque_lucid_june_2,
                           variables=c("socialmedia",  "broadcast_tv", "info_CNN",
                                       "info_FOX", "info_MSNBC", "info_radio_npr",
                                       "info_radio_hannity", "info_localnewspaper", 
                                       "info_NYT","info_wsj"), change = "minmax"))) %>% 
                           select(c("AME","lower","upper","factor"))

margin_junelucid_weapon <- as.data.frame(summary(margins(weapon_lucid_june_2,
                           variables=c("socialmedia",  "broadcast_tv", "info_CNN", 
                                       "info_FOX", "info_MSNBC", "info_radio_npr", 
                                       "info_radio_hannity", "info_localnewspaper",
                                       "info_NYT","info_wsj"), change = "minmax"))) %>% 
                          select(c("AME","lower","upper", "factor"))




############
# Figure 1 #
############

# rbind marginal effects 
marginal_fx_ssi<- rbind(margin_ssi_ban, margin_ssi_patrol)

# IVs included in plot
var<-rep(c("Cable TV", "Local TV","Newspaper","Radio","Social Media"), times=2) 

# DVs included in plot
dep_var<-rep(c("1","2"),each=5)
#final data
df_ssi<-cbind(marginal_fx_ssi,var,dep_var)
#lables
lab <- c(
  "1" = "Muslim Ban",
  "2" = "Police Patrols")

#plot
ggplot(data = df_ssi, aes(x = var%>%fct_relevel(c("Social Media","Cable TV", 
                       "Local TV", "Radio", "Newspaper"))%>%fct_rev, y = AME,ymin=lower, ymax = upper))+
  facet_grid(.~dep_var, labeller = as_labeller(lab))+
  geom_pointrange(size=.75,position=position_dodge(width=.5))+
  theme_bw(base_size=18,base_family="Times")+
  coord_flip()+
  #scale_y_continuous(limits=c(-.11,.11), breaks=seq( -.1, .1, .05,))+
  theme(panel.grid.major.x = element_blank(),panel.grid.major.y = element_blank())+
  theme(strip.background = element_rect(fill="white"))+
  theme(strip.text.x = element_text(size = 19))+
  theme(legend.title=element_blank(),
        legend.key.size=unit(2.0, 'lines'))+
  geom_hline(yintercept=0, color="maroon", size=.5, linetype = 2) +
  theme(plot.margin=unit(c(1,2,1,1),"cm"))+
  labs(y="Change in Predicted Value of Support (Min-Max)",x=NULL,title=NULL)
  ggsave("Figure1.png", width = 8, height = 8)
  
############
# Figure 3 #
############
marginal_fx_marchlucid<- rbind(margin_marchlucid_ban, margin_marchlucid_patrol)
  
# IVs included in plot
var<-rep(c("Broadcast TV", "CNN","FOX","Local Newspaper","MSNBC", "NYT", "Radio", "Social Media"), times=2) 
  
# DVs included in plot
dep_var<-rep(c("1","2"),each=8)
#final data
df_marchlucid<-cbind(marginal_fx_marchlucid,var,dep_var)
#labels
lab <- c(
    "1" = "Muslim Ban",
    "2" = "Police Patrols"
  )
  
#plot
ggplot(data = df_marchlucid, aes(x = var%>%fct_relevel(c("Social Media",
                  "FOX", "CNN", "MSNBC", "Broadcast TV", "Radio", 
                  "Local Newspaper", "NYT"))%>%fct_rev, y = AME,ymin=lower, ymax = upper))+
    facet_grid(.~dep_var, labeller = as_labeller(lab))+
    geom_pointrange(size=.75,position=position_dodge(width=.5))+
    theme_bw(base_size=18,base_family="Times")+
    coord_flip()+
    theme(panel.grid.major.x = element_blank(),panel.grid.major.y = element_blank())+
    theme(strip.background = element_rect(fill="white"))+
    theme(strip.text.x = element_text(size = 19))+
    theme(legend.title=element_blank(),
          legend.key.size=unit(2.0, 'lines'))+
    geom_hline(yintercept=0, color="maroon", size=.5, linetype = 2) +
    theme(plot.margin=unit(c(1,2,1,1),"cm"))+
    labs(y="Change in Predicted Value of Support (Min-Max)",x=NULL,title=NULL)
    ggsave("Figure3.png", width = 8, height = 8)

############
# Figure 5 #
############  

marginal_fx_junelucid<- rbind(margin_junelucid_ban, 
                              margin_junelucid_patrol,
                              margin_junelucid_mosque,
                              margin_junelucid_weapon)

# IVs included in plot
var<-rep(c("Broadcast TV", "CNN","FOX","Local Newspaper",
           "MSNBC", "NYT", "Radio:Hannity","Radio:NPR",
           "WSJ", "Social Media"), times=4) 

# DVs included in the plot
dep_var<-rep(c("1","2","3","4"),each=10)
# final plot data
df_junelucid<-cbind(marginal_fx_junelucid,var,dep_var)
#labels
lab <- c(
  "1" = "Muslim Ban",
  "2" = "Police Patrols",
  "3" = "Restrict Mosques",
  "4" = "Weapon Ban"
)

# plot
ggplot(data = df_junelucid, aes(x = var%>%fct_relevel(c("Social Media","FOX", 
             "CNN", "MSNBC", "Broadcast TV", "Radio:Hannity", "Radio:NPR", 
             "Local Newspaper", "WSJ", "NYT"))%>%fct_rev, y = AME,ymin=lower, ymax = upper))+
  facet_grid(.~dep_var, labeller = as_labeller(lab))+
  geom_pointrange(size=.65, position=position_dodge(width=.5))+
  theme_bw(base_size=22,base_family="Times")+
  coord_flip()+
  theme(panel.grid.major.x = element_blank(),panel.grid.major.y = element_blank())+
  theme(strip.background = element_rect(fill="white"))+
  theme(strip.text.x = element_text(size = 22))+
  theme(legend.title=element_blank(),
        legend.key.size=unit(2.0, 'lines'))+
	geom_hline(yintercept=0, color="maroon", size=.5, linetype = 2) +
  theme(plot.margin=unit(c(1,2,1,1),"cm"))+
  labs(y="Change in Predicted Value of Support (Min-Max)",x=NULL,title=NULL)
  ggsave("Figure5.png", width = 15, height = 12)

  
############
# Table B6 #
############
#Ban SSI Dems
summary(ban_ssi_dem <-
  lm(ban ~ socialmedia + female + age + white+ income+ educ + localTV +
           cableTV + radio+ newspaper_online_print,    
                  data = ssi_dem))

#Patrol SSI Dems
summary(patrol_ssi_dem <-
  lm(patrol ~ socialmedia + female + age + white+ income+ educ + localTV +
              cableTV + radio+ newspaper_online_print,    
                  data = ssi_dem))

#Ban SSI Reps
summary(ban_ssi_rep <-
  lm(ban ~ socialmedia + female + age + white+ income+ educ + localTV +
            cableTV + radio+ newspaper_online_print,    
                  data = ssi_rep))

#Patrol SSI Reps
summary(patrol_ssi_rep <-
  lm(patrol ~ socialmedia + female + age + white+ income+ educ + localTV +
              cableTV + radio+ newspaper_online_print,    
                  data = ssi_rep))  
  
tab_labels <- c( "Social Media", "Female", "Age",  "White", "Income", 
                 "Education", "Broadcast TV", "Cable TV", "Radio", "Newspaper")

stargazer(ban_ssi_dem, ban_ssi_rep, patrol_ssi_dem, patrol_ssi_rep, 
          style="ajps", out.header=T,
           model.numbers = TRUE,
         covariate.labels = tab_labels,
          dep.var.labels =  c("Support Ban", "Support Patrol"),
          out = "table_ssi_pid.tex", 
          label = "table_ssi_pid",
          font.size = "footnotesize",
         column.sep.width = "2pt",
         omit.stat = c("ser","f")
          )  


####################################
# SSI PID Predictions for Plotting #
####################################
margin_ssi_ban_dem <- as.data.frame(summary(margins(ban_ssi_dem,
                      variables=c("socialmedia"), change = "minmax"))) %>% 
                      select(c("AME","lower","upper", "factor"))

margin_ssi_ban_dem <- margin_ssi_ban_dem %>% 
  mutate(DV="01Ban", IV="Social Media", Party="Democrats") %>% 
  dplyr::select("AME","lower","upper", "factor", DV, IV, Party) 


margin_ssi_patrol_dem <- as.data.frame(summary(margins(patrol_ssi_dem,
                         variables=c("socialmedia"), change = "minmax"))) %>% 
                         select(c("AME","lower","upper", "factor"))

margin_ssi_patrol_dem <- margin_ssi_patrol_dem %>% 
  mutate(DV="02Patrol", IV="Social Media", Party="Democrats") %>% 
  dplyr::select("AME","lower","upper", "factor", DV, IV, Party) 


margin_ssi_ban_rep <- as.data.frame(summary(margins(ban_ssi_rep,
                      variables=c("socialmedia"), change = "minmax"))) %>% 
                      select(c("AME","lower","upper", "factor"))

margin_ssi_ban_rep <- margin_ssi_ban_rep %>% 
  mutate(DV="01Ban", IV="Social Media", Party="Republicans") %>% 
  dplyr::select("AME","lower","upper", "factor", DV, IV, Party) 


margin_ssi_patrol_rep <- as.data.frame(summary(margins(patrol_ssi_rep,
                         variables=c("socialmedia"), change = "minmax"))) %>% 
                         select(c("AME","lower","upper", "factor"))

margin_ssi_patrol_rep <- margin_ssi_patrol_rep %>% 
  mutate(DV="02Patrol", IV="Social Media", Party="Republicans") %>% 
  dplyr::select("AME","lower","upper", "factor", DV, IV, Party)   

############
# Figure 2 #
############  

marginal_fx_ssi_party<- rbind(margin_ssi_ban_dem, margin_ssi_ban_rep,
                              margin_ssi_patrol_dem, margin_ssi_patrol_rep)


# DVs included in plot
dep_var<-rep(c("1","2"),each=2)
#final data
marginal_fx_ssi_party<-cbind(marginal_fx_ssi_party,dep_var)
#labels
lab <- c(
  "1" = "Muslim Ban",
  "2" = "Police Patrols")

#plot
ggplot(data = marginal_fx_ssi_party, aes(x = IV, y = AME, ymin=lower, ymax = upper, color=Party, shape=Party))+
  facet_grid(.~dep_var, labeller = as_labeller(lab))+
  geom_pointrange(size=.8, position=position_dodge(width=.2), alpha=.9)+
  theme_bw(base_size=15,base_family="Times")+
  coord_flip()+
    scale_color_brewer(palette = "Set1", direction = -1) +
  scale_y_continuous(limits=c(-.1,.3), breaks=seq( -.1, .3, .1,))+
  theme(panel.grid.major.x = element_blank(), panel.grid.major.y = element_blank())+
  theme(strip.background = element_rect(fill="white"))+
  theme(strip.text.x = element_text(size = 15))+
  theme(legend.title=element_blank(),
        legend.key.size=unit(2.0, 'lines'), legend.direction='vertical')+
	geom_hline(yintercept=0, color="maroon", size=.5, linetype = 2) +
  theme(plot.margin=unit(c(1,1,1,1),"cm"))+
  labs(y="Change in Predicted Value of Support (Min-Max)",x=NULL,title=NULL)
  ggsave("Figure2.png", width = 8, height = 4.5)  

  
############
# Table B7 #
############ 
#Ban Dems
summary(ban_lucid_march_dem <-
  lm(ban ~ socialmedia + ideology_7pt+ female + age + white+ income+ educ + 
       fav_MuslimAmericans+ broadcast_tv+ info_CNN + info_FOX + info_MSNBC + 
       info_radio+ info_localnewspaper+ info_NYT, data = lucid_march_dem))

#Patrol Dems
summary(patrol_lucid_march_dem <-
  lm(patrol ~ socialmedia + ideology_7pt+ female + age + white+ income+ educ + 
       fav_MuslimAmericans+ broadcast_tv+ info_CNN + info_FOX + info_MSNBC + 
       info_radio+ info_localnewspaper+ info_NYT, data = lucid_march_dem))

#Ban Reps
summary(ban_lucid_march_rep <-
  lm(ban ~ socialmedia + ideology_7pt+ female + age + white+ income+ educ + 
       fav_MuslimAmericans+ broadcast_tv+ info_CNN + info_FOX + info_MSNBC + 
       info_radio+ info_localnewspaper+ info_NYT, data = lucid_march_rep))

#Patrol Reps
summary(patrol_lucid_march_rep <-
  lm(patrol ~ socialmedia + ideology_7pt+ female + age + white+ income+ educ + 
       fav_MuslimAmericans+ broadcast_tv+ info_CNN + info_FOX + info_MSNBC + 
       info_radio+ info_localnewspaper+ info_NYT, data = lucid_march_rep))

tab_labels <- c( "Social Media", "Ideology (Lib-Con)", "Female",  
                 "Age",  "White", "Income", "Education", "Muslim Favorability", 
                 "Broadcast TV", "CNN", "FOX", "MSNBC", "Radio", 
                 "Local Newspaper", "NYT")

stargazer(ban_lucid_march_dem, ban_lucid_march_rep, patrol_lucid_march_dem, 
          patrol_lucid_march_rep, style="ajps", out.header=T,
           model.numbers = TRUE,
         covariate.labels = tab_labels,
          dep.var.labels =  c("Support Ban", "Support Patrol"),
          out = "table_lucid_march_pid.tex", 
          label = "table_lucid_march_pid",
          font.size = "footnotesize",
         column.sep.width = "2pt",
         omit.stat = c("ser","f")
          )
  
  
############################################
# March Lucid PID Predictions for Plotting #
############################################
margin_lucid_march_ban_dem <- as.data.frame(summary(margins(ban_lucid_march_dem,
                              variables=c("socialmedia"), change = "minmax"))) %>% 
                              select(c("AME","lower","upper", "factor"))

margin_lucid_march_ban_dem <- margin_lucid_march_ban_dem %>% 
  mutate(DV="01Ban", IV="Social Media", Party="Democrats") %>% 
  dplyr::select("AME","lower","upper", "factor", DV, IV, Party) 


margin_lucid_march_patrol_dem <- as.data.frame(summary(margins(patrol_lucid_march_dem,
                                 variables=c("socialmedia"), change = "minmax"))) %>% 
                                 select(c("AME","lower","upper", "factor"))

margin_lucid_march_patrol_dem <- margin_lucid_march_patrol_dem %>% 
  mutate(DV="02Patrol", IV="Social Media", Party="Democrats") %>% 
  dplyr::select("AME","lower","upper", "factor", DV, IV, Party) 


margin_lucid_march_ban_rep<- as.data.frame(summary(margins(ban_lucid_march_rep,
                             variables=c("socialmedia"), change = "minmax"))) %>% 
                             select(c("AME","lower","upper", "factor"))

margin_lucid_march_ban_rep <- margin_lucid_march_ban_rep %>% 
  mutate(DV="01Ban", IV="Social Media", Party="Republicans") %>% 
  dplyr::select("AME","lower","upper", "factor", DV, IV, Party) 


margin_lucid_march_patrol_rep <- as.data.frame(summary(margins(patrol_lucid_march_rep,
                                 variables=c("socialmedia"), change = "minmax"))) %>% 
                                 select(c("AME","lower","upper", "factor"))

margin_lucid_march_patrol_rep <- margin_lucid_march_patrol_rep %>% 
  mutate(DV="02Patrol", IV="Social Media", Party="Republicans") %>% 
  dplyr::select("AME","lower","upper", "factor", DV, IV, Party) 

############
# Figure 4 #
############ 

marginal_fx_lucid_march_party<- rbind(margin_lucid_march_ban_dem, 
                                      margin_lucid_march_ban_rep,
                                      margin_lucid_march_patrol_dem, 
                                      margin_lucid_march_patrol_rep)


# DVs included in plot
dep_var<-rep(c("1","2"),each=2)

#final data
marginal_fx_lucid_march_party<-cbind(marginal_fx_lucid_march_party,dep_var)

#labels
lab <- c(
  "1" = "Muslim Ban",
  "2" = "Police Patrols")

#plot
ggplot(data = marginal_fx_lucid_march_party, aes(x = IV, y = AME, ymin=lower, ymax = upper, color=Party, shape=Party))+
  facet_grid(.~dep_var, labeller = as_labeller(lab))+
  geom_pointrange(size=.8, position=position_dodge(width=.2), alpha=.9)+
  theme_bw(base_size=15,base_family="Times")+
  coord_flip()+
    scale_color_brewer(palette = "Set1", direction = -1) +
  scale_y_continuous(limits=c(-.1,.4), breaks=seq( -.1, .4, .1,))+
  theme(panel.grid.major.x = element_blank(), panel.grid.major.y = element_blank())+
  theme(strip.background = element_rect(fill="white"))+
  theme(strip.text.x = element_text(size = 15))+
  theme(legend.title=element_blank(),
        legend.key.size=unit(2.0, 'lines'), legend.direction='vertical')+
	geom_hline(yintercept=0, color="maroon", size=.5, linetype = 2) +
  theme(plot.margin=unit(c(1,1,1,1),"cm"))+
  labs(y="Change in Predicted Value of Support (Min-Max)",x=NULL,title=NULL)
  ggsave("Figure4.png", width = 8, height = 4.5)


  
  
############
# Table B8 #
############ 
#Ban Dems
summary(ban_lucid_june_dem <-
  lm(ban ~ socialmedia + ideology_7pt+ female + age + white+ income+ educ + 
       fav_MuslimAmericans+ broadcast_tv+ info_CNN + info_FOX + info_MSNBC +
       info_radio_npr+ info_radio_hannity+ info_localnewspaper+ info_NYT +
       info_wsj, data = lucid_june_dem))

#Patrol Dems
summary(patrol_lucid_june_dem <-
  lm(patrol ~ socialmedia + ideology_7pt+ female + age + white+ income+ educ + 
       fav_MuslimAmericans+ broadcast_tv+ info_CNN + info_FOX + info_MSNBC + 
       info_radio_npr+ info_radio_hannity+ info_localnewspaper+ info_NYT +
       info_wsj, data = lucid_june_dem))

#Mosque Dems
summary(mosque_lucid_june_dem <-
  lm(restrict_mosques ~ socialmedia + ideology_7pt+ female + age + white+ 
       income+ educ + fav_MuslimAmericans+ broadcast_tv+ info_CNN + info_FOX + 
       info_MSNBC + info_radio_npr+ info_radio_hannity+ info_localnewspaper+ 
       info_NYT +info_wsj, data = lucid_june_dem))

#Weapon Dems
summary(weapon_lucid_june_dem <-
  lm(noweapon_muslims ~ socialmedia + ideology_7pt+ female + age + white+ 
       income+ educ + fav_MuslimAmericans+ broadcast_tv+ info_CNN + info_FOX + 
       info_MSNBC + info_radio_npr+ info_radio_hannity+ info_localnewspaper+ 
       info_NYT +info_wsj, data = lucid_june_dem))


#Ban Reps
summary(ban_lucid_june_rep <-
  lm(ban ~ socialmedia + ideology_7pt+ female + age + white+ income+ educ + 
       fav_MuslimAmericans+ broadcast_tv+ info_CNN + info_FOX + info_MSNBC + 
       info_radio_npr+ info_radio_hannity+ info_localnewspaper+ info_NYT +
       info_wsj, data = lucid_june_rep))

#Patrol Reps
summary(patrol_lucid_june_rep <-
  lm(patrol ~ socialmedia + ideology_7pt+ female + age + white+ income+ educ + 
       fav_MuslimAmericans+ broadcast_tv+ info_CNN + info_FOX + info_MSNBC + 
       info_radio_npr+ info_radio_hannity+ info_localnewspaper+ info_NYT +
       info_wsj, data = lucid_june_rep))

#Mosque rep
summary(mosque_lucid_june_rep <-
  lm(restrict_mosques ~ socialmedia + ideology_7pt+ female + age + white+ 
       income+ educ + fav_MuslimAmericans+ broadcast_tv+ info_CNN + info_FOX + 
       info_MSNBC + info_radio_npr+ info_radio_hannity+ info_localnewspaper+ 
       info_NYT +info_wsj, data = lucid_june_rep))

#Weapon rep
summary(weapon_lucid_june_rep <-
  lm(noweapon_muslims ~ socialmedia + ideology_7pt+ female + age + white+ 
       income+ educ + fav_MuslimAmericans+ broadcast_tv+ info_CNN + info_FOX + 
       info_MSNBC + info_radio_npr+ info_radio_hannity+ info_localnewspaper+ 
       info_NYT +info_wsj, data = lucid_june_rep))


tab_labels <- c( "Social Media", "Ideology (Lib-Con)", "Female",  
                 "Age",  "White", "Income", "Education", "Muslim Favorability", 
                 "Broadcast TV", "CNN", "FOX", "MSNBC", "Radio NPR", 
                 "Radio Hannity", "Local Newspaper", "NYT", "WSJ")

stargazer(ban_lucid_june_dem, ban_lucid_june_rep, patrol_lucid_june_dem, 
          patrol_lucid_june_rep, mosque_lucid_june_dem, mosque_lucid_june_rep, 
          weapon_lucid_june_dem, weapon_lucid_june_rep, 
          style="ajps", out.header=T,
           model.numbers = TRUE,
         covariate.labels = tab_labels,
          dep.var.labels =  c("Support Ban", "Support Patrol", "Restrict Mosque", "Restrict Weapon"),
          out = "table_lucid_june_pid.tex", 
          label = "table_lucid_june_pid",
          font.size = "footnotesize",
         column.sep.width = "2pt",
         omit.stat = c("ser","f")
          )

############################################
# June Lucid PID Predictions for Plotting #
############################################
#Ban DEM
margin_lucid_june_ban_dem <- as.data.frame(summary(margins(ban_lucid_june_dem,
                             variables=c("socialmedia"), change = "minmax"))) %>% 
                             select(c("AME","lower","upper", "factor"))

margin_lucid_june_ban_dem <- margin_lucid_june_ban_dem %>% 
  mutate(DV="01Ban", IV="Social Media", Party="Democrats") %>% 
  dplyr::select("AME","lower","upper", "factor", DV, IV, Party) 

#Patrol DEM
margin_lucid_june_patrol_dem <- as.data.frame(summary(margins(patrol_lucid_june_dem,
                                variables=c("socialmedia"), change = "minmax"))) %>% 
                                select(c("AME","lower","upper", "factor"))

margin_lucid_june_patrol_dem <- margin_lucid_june_patrol_dem %>% 
  mutate(DV="02Patrol", IV="Social Media", Party="Democrats") %>% 
  dplyr::select("AME","lower","upper", "factor", DV, IV, Party) 

#Mosque Dem
margin_lucid_june_mosque_dem <- as.data.frame(summary(margins(mosque_lucid_june_dem,
                                variables=c("socialmedia"), change = "minmax"))) %>% 
                                select(c("AME","lower","upper", "factor"))

margin_lucid_june_mosque_dem <- margin_lucid_june_mosque_dem %>% 
  mutate(DV="03Mosque", IV="Social Media", Party="Democrats") %>% 
  dplyr::select("AME","lower","upper", "factor", DV, IV, Party) 

#Weapon Dem
margin_lucid_june_weapon_dem <- as.data.frame(summary(margins(weapon_lucid_june_dem,
                                variables=c("socialmedia"), change = "minmax"))) %>% 
                                select(c("AME","lower","upper", "factor"))

margin_lucid_june_weapon_dem <- margin_lucid_june_weapon_dem %>% 
  mutate(DV="04Weapon", IV="Social Media", Party="Democrats") %>% 
  dplyr::select("AME","lower","upper", "factor", DV, IV, Party) 

#Ban REP
margin_lucid_june_ban_rep<- as.data.frame(summary(margins(ban_lucid_june_rep,
                            variables=c("socialmedia"), change = "minmax"))) %>% 
                            select(c("AME","lower","upper", "factor"))

margin_lucid_june_ban_rep <- margin_lucid_june_ban_rep %>% 
  mutate(DV="01Ban", IV="Social Media", Party="Republicans") %>% 
  dplyr::select("AME","lower","upper", "factor", DV, IV, Party) 

#Patrol REP
margin_lucid_june_patrol_rep <- as.data.frame(summary(margins(patrol_lucid_june_rep,
                                variables=c("socialmedia"), change = "minmax"))) %>% 
                                select(c("AME","lower","upper", "factor"))

margin_lucid_june_patrol_rep <- margin_lucid_june_patrol_rep %>% 
  mutate(DV="02Patrol", IV="Social Media", Party="Republicans") %>% 
  dplyr::select("AME","lower","upper", "factor", DV, IV, Party) 

#Mosque REP
margin_lucid_june_mosque_rep <- as.data.frame(summary(margins(mosque_lucid_june_rep,
                                variables=c("socialmedia"), change = "minmax"))) %>% 
                                select(c("AME","lower","upper", "factor"))

margin_lucid_june_mosque_rep <- margin_lucid_june_mosque_rep %>% 
  mutate(DV="03Mosque", IV="Social Media", Party="Republicans") %>% 
  dplyr::select("AME","lower","upper", "factor", DV, IV, Party) 

#Weapon REP
margin_lucid_june_weapon_rep <- as.data.frame(summary(margins(weapon_lucid_june_rep,
                                variables=c("socialmedia"), change = "minmax"))) %>% 
                                select(c("AME","lower","upper", "factor"))

margin_lucid_june_weapon_rep <- margin_lucid_june_weapon_rep %>% 
  mutate(DV="04Weapon", IV="Social Media", Party="Republicans") %>% 
  dplyr::select("AME","lower","upper", "factor", DV, IV, Party) 

############
# Figure 6 #
############
marginal_fx_lucid_june_party<- rbind(margin_lucid_june_ban_dem, 
                                     margin_lucid_june_ban_rep,
                                     margin_lucid_june_patrol_dem, 
                                     margin_lucid_june_patrol_rep, 
                                     margin_lucid_june_mosque_dem, 
                                     margin_lucid_june_mosque_rep, 
                                     margin_lucid_june_weapon_dem, 
                                     margin_lucid_june_weapon_rep)


# DVs included in plot
dep_var<-rep(c("1","2", "3", "4"),each=2)

#final data
marginal_fx_lucid_june_party<-cbind(marginal_fx_lucid_june_party,dep_var)

#labels
lab <- c(
  "1" = "Muslim Ban",
  "2" = "Police Patrols", 
  "3" = "Restrict Mosques",
  "4" = "Weapon Ban")

#plot
ggplot(data = marginal_fx_lucid_june_party, aes(x = IV, y = AME, 
                                                ymin=lower, 
                                                ymax = upper, 
                                                color=Party, 
                                                shape=Party))+
  facet_grid(.~dep_var, labeller = as_labeller(lab))+
  geom_pointrange(size=.8, position=position_dodge(width=.2), alpha=.9)+
  theme_bw(base_size=15,base_family="Times")+
  coord_flip()+
    scale_color_brewer(palette = "Set1", direction = -1) +
  scale_y_continuous(limits=c(-.1,.35), breaks=seq( -.1, .4, .1,))+
  theme(panel.grid.major.x = element_blank(), panel.grid.major.y = element_blank())+
  theme(strip.background = element_rect(fill="white"))+
  theme(strip.text.x = element_text(size = 15))+
  theme(legend.title=element_blank(),
        legend.key.size=unit(2.0, 'lines'), legend.direction='vertical')+
	geom_hline(yintercept=0, color="maroon", size=.5, linetype = 2) +
  theme(plot.margin=unit(c(1,2,1,1),"cm"))+
  labs(y="Change in Predicted Value of Support (Min-Max)",x=NULL,title=NULL)
  ggsave("Figure6.png", width = 12, height = 4.5)

  
###################  
#Tables C5 and C6 #
###################
summary(ban_lucid_march_twit_face <-
  lm(ban ~ info_Twitter + info_Facebook + republican +  independent + 
       ideology_7pt+ female + age + white+ income+ educ + fav_MuslimAmericans+ 
       broadcast_tv+ info_CNN + info_FOX + info_MSNBC + info_radio+ 
       info_localnewspaper+ info_NYT, data = lucid_march_final))

summary(patrol_lucid_march_twit_face <-
  lm(patrol ~ info_Twitter + info_Facebook + republican +  independent + 
       ideology_7pt+ female + age + white+ income+ educ + fav_MuslimAmericans+ 
       broadcast_tv+ info_CNN + info_FOX + info_MSNBC + info_radio+ 
       info_localnewspaper+ info_NYT, data = lucid_march_final))

summary(ban_lucid_june_twit_face <-
  lm(ban ~ info_Twitter + info_Facebook + republican +  independent + 
       ideology_7pt+ female + age + white+ income+ educ + fav_MuslimAmericans+ 
       broadcast_tv+ info_CNN + info_FOX + info_MSNBC + info_radio_npr+
       info_radio_hannity +info_localnewspaper+ info_NYT + info_wsj, 
     data = lucid_june_final))

summary(patrol_lucid_june_twit_face <-
  lm(patrol ~ info_Twitter + info_Facebook + republican +  independent + 
       ideology_7pt+ female + age + white+ income+ educ + fav_MuslimAmericans+ 
       broadcast_tv+ info_CNN + info_FOX + info_MSNBC + info_radio_npr+
       info_radio_hannity+ info_localnewspaper+ info_NYT+info_wsj, 
     data = lucid_june_final))

summary(mosque_lucid_june_twit_face<-
  lm(restrict_mosques ~ info_Twitter + info_Facebook + republican + independent + 
       ideology_7pt+ female + age + white+ income+ educ + fav_MuslimAmericans+ 
       broadcast_tv+ info_CNN + info_FOX + info_MSNBC + info_radio_npr+
       info_radio_hannity+ info_localnewspaper+ info_NYT+info_wsj, 
     data = lucid_june_final))

summary(noweapon_lucid_june_twit_face<-
  lm(noweapon_muslims ~ info_Twitter + info_Facebook + republican + independent + 
       ideology_7pt+ female + age + white+ income+ educ + fav_MuslimAmericans+ 
       broadcast_tv+ info_CNN + info_FOX + info_MSNBC + info_radio_npr+
       info_radio_hannity+ info_localnewspaper+ info_NYT+info_wsj, 
     data = lucid_june_final))

tab_labels <- c( "Twitter", "Facebook", "Republican" ,  "Independent", 
                 "Ideology (Lib-Con)", "Female", "Age",  "White", "Income", 
                 "Education", "Muslim Favorability", "Broadcast TV", "CNN", 
                 "FOX", "MSNBC", "Radio", "Local Newspaper", "NYT")

stargazer(ban_lucid_march_twit_face, patrol_lucid_march_twit_face, style="ajps", out.header=T,
           model.numbers = TRUE,
         covariate.labels = tab_labels,
          dep.var.labels =  c("Ban", "Patrol"),
          out = "table_lucid_march_twitter_facebook.tex", 
          label = "table_lucid_march_twitter_facebook",
          font.size = "footnotesize",
         column.sep.width = "2pt",
         omit.stat = c("ser","f")
          )


tab_labels2 <- c( "Twitter", "Facebook", "Republican", "Independent",
                  "Ideology (Lib-Con)", "Female", "Age",  "White", 
                  "Income", "Education", "Muslim Favorability", 
                  "Broadcast TV", "CNN", "FOX", "MSNBC", "Radio NPR", 
                  "Radio Hannity", "Local Newspaper", "NYT", "WSJ")

stargazer(ban_lucid_june_twit_face, patrol_lucid_june_twit_face, mosque_lucid_june_twit_face, noweapon_lucid_june_twit_face, style="ajps", out.header=T,
           model.numbers = TRUE,
         covariate.labels = tab_labels2,
          dep.var.labels =  c("Ban", "Patrol", "Mosque", "Weapon"),
          out = "table_lucid_june_twitter_facebook.tex", 
          label = "table_lucid_june_twitter_facebook",
          font.size = "footnotesize",
         column.sep.width = "2pt",
         omit.stat = c("ser","f")
          )



#############################
#############################
#############################
#############################
#############################

###########################
#SSI Weight Creation
###########################
#create age groups for weighted analysis 
ssi_final <- ssi_final %>% mutate(agegroup = case_when(age >= 18  & age <= 29 ~ '0',
                                             age >= 30  & age <= 39 ~ '1',
                                             age >= 40  & age <= 49 ~ '2', 
                                             age >= 50  & age <= 59 ~ '3', 
                                             age >= 60  & age <= 69 ~ '4', 
                                             age >= 70   ~ '5'))

ssi_final$agegroup<- as.numeric(ssi_final$agegroup)

# Set base weight to 1
ssi_final$weight <- 1 

# Create svydesign object
ssi_weight_data <- svydesign(id = ~1, weights=~weight, data = ssi_final)

#####################
# 2016 ACS 5-year estimates sex, age, education, race 
#####################
#sex
pop.types_female <- data.frame(female = c(0, 1), Freq = unlist(c(nrow(ssi_final)*.492, nrow(ssi_final)*.508)))

#No HS, HS grad, Some College, 2-Year College, 4-Year, PostGrad
pop.types_educ<- data.frame(educ = c(1:6), Freq = unlist(c(nrow(ssi_final)*.13, nrow(ssi_final)*.275, nrow(ssi_final)*.21, nrow(ssi_final)*.082, nrow(ssi_final)*.188, nrow(ssi_final)*.115)))

#white, non-hispanic 
pop.types_white <- data.frame(white = c(0,1), Freq = unlist(c(nrow(ssi_final)*.38, nrow(ssi_final)*.62) ))

#age 
pop.types_agegroup<-data.frame(agegroup=c(0:5), Freq = unlist(c(nrow(ssi_final)*0.217556764, nrow(ssi_final)*0.169161798, nrow(ssi_final)*0.169912143, nrow(ssi_final)*0.178204997, nrow(ssi_final)*0.139156387, nrow(ssi_final)*0.126007911)))


####################
# Rake the Weights #
####################
ssi_weight_data <- rake(ssi_weight_data, list(~female, ~educ, ~white, ~agegroup), 
                        list(pop.types_female, pop.types_educ, 
                             pop.types_white, pop.types_agegroup))


ssi_final$weight <- 1/ssi_weight_data$prob
hist(ssi_final$weight); mean(ssi_final$weight)

summary(ssi_final$weight)


#Trim weights
ssi_weight_data_trim <- trimWeights(ssi_weight_data, upper=5,
                               strict=TRUE)


ssi_final$weight <- 1/ssi_weight_data_trim$prob
hist(ssi_final$weight); mean(ssi_final$weight)

summary(ssi_final$weight)

write.csv(ssi_final, file="ssi_weight_trimmed.csv", row.names = F)


################################
# Confirm Survey Weight Results 
################################

# Sex #
#Weighted mean
crosstab(ssi_final$female, rep(1, nrow(ssi_final)), weight = ssi_final$weight, plot=F, prop.r=T)
#unweighted mean
CrossTable(ssi_final$female)

# Education #
crosstab(ssi_final$educ, rep(1, nrow(ssi_final)), weight = ssi_final$weight, plot=F, prop.r=T)
CrossTable(ssi_final$educ)

# White#
crosstab(ssi_final$white, rep(1, nrow(ssi_final)), weight = ssi_final$weight, plot=F, prop.r=T)
CrossTable(ssi_final$white)

# Age$
crosstab(ssi_final$agegroup, rep(1, nrow(ssi_final)), weight = ssi_final$weight, plot=F, prop.r=T)
CrossTable(ssi_final$agegroup)


##########################################
#LUCID March weight Creation
##########################################
#create age groups for weighted analysis 
lucid_march_final <- lucid_march_final %>% mutate(agegroup = case_when(age >= 18  & age <= 29 ~ '0',
                                             age >= 30  & age <= 39 ~ '1',
                                             age >= 40  & age <= 49 ~ '2', 
                                             age >= 50  & age <= 59 ~ '3', 
                                             age >= 60  & age <= 69 ~ '4', 
                                             age >= 70   ~ '5'))

lucid_march_final$agegroup<- as.numeric(lucid_march_final$agegroup)

# Set base weight to 1
lucid_march_final$weight <- 1 

# Create svydesign object
lucid_march_weight_data <- svydesign(id = ~1, weights=~weight, data = lucid_march_final)

#####################
# 2019 ACS 5-year estimates sex, age, education, race 
#####################
pop.types_female <- data.frame(female = c(0, 1), Freq = unlist(c(nrow(lucid_march_final)*.492, nrow(lucid_march_final)*.508)))

#No HS, HS grad, Some College, 2-Year College, 4-Year, PostGrad
pop.types_educ<- data.frame(educ = c(1:6), Freq = unlist(c(nrow(lucid_march_final)*.120, nrow(lucid_march_final)*.270, nrow(lucid_march_final)*.204, nrow(lucid_march_final)*.085, nrow(lucid_march_final)*.198, nrow(lucid_march_final)*.124)))

#white, non-hispanic 
pop.types_white <- data.frame(white = c(0,1), Freq = unlist(c(nrow(lucid_march_final)*.393, nrow(lucid_march_final)*.607)))

#age 
pop.types_agegroup<-data.frame(agegroup=c(0:5), Freq = unlist(c(nrow(lucid_march_final)*0.214, nrow(lucid_march_final)*0.171, nrow(lucid_march_final)*0.162, nrow(lucid_march_final)*0.171, nrow(lucid_march_final)*0.147, nrow(lucid_march_final)*0.135)))

####################
# Rake the Weights #
####################
lucid_march_weight_data <- rake(lucid_march_weight_data, 
                                list(~female, ~educ, ~white, ~agegroup), 
                                list(pop.types_female, pop.types_educ, 
                                     pop.types_white, pop.types_agegroup))


lucid_march_final$weight <- 1/lucid_march_weight_data$prob
hist(lucid_march_final$weight); mean(lucid_march_final$weight)

summary(lucid_march_final$weight)


#Trim weights
lucid_march_weight_data_trim <- trimWeights(lucid_march_weight_data, upper=5,
                               strict=TRUE)

lucid_march_final$weight <- 1/lucid_march_weight_data_trim$prob
hist(lucid_march_final$weight); mean(lucid_march_final$weight)

summary(lucid_march_final$weight)


write.csv(lucid_march_final, file="lucid_march_weight.csv", row.names = F)

################################
# Confirm Survey Weight Results 
################################

# Female #
crosstab(lucid_march_final$female, rep(1, nrow(lucid_march_final)), weight = lucid_march_final$weight, plot=F, prop.r=T)
CrossTable(lucid_march_final$female)

# Education #
crosstab(lucid_march_final$educ, rep(1, nrow(lucid_march_final)), weight = lucid_march_final$weight, plot=F, prop.r=T)
CrossTable(lucid_march_final$educ)

# White#
crosstab(lucid_march_final$white, rep(1, nrow(lucid_march_final)), weight = lucid_march_final$weight, plot=F, prop.r=T)
CrossTable(lucid_march_final$white)

# Age #
crosstab(lucid_march_final$agegroup, rep(1, nrow(lucid_march_final)), weight = lucid_march_final$weight, plot=F, prop.r=T)
CrossTable(lucid_march_final$agegroup)


##########################################
#LUCID June weight Creation
##########################################
#create age groups for weighted analysis 
lucid_june_final <- lucid_june_final %>% mutate(agegroup = case_when(age >= 18  & age <= 29 ~ '0',
                                             age >= 30  & age <= 39 ~ '1',
                                             age >= 40  & age <= 49 ~ '2', 
                                             age >= 50  & age <= 59 ~ '3', 
                                             age >= 60  & age <= 69 ~ '4', 
                                             age >= 70   ~ '5'))

lucid_june_final$agegroup<- as.numeric(lucid_june_final$agegroup)

# Set base weight to 1
lucid_june_final$weight <- 1 

# Create svydesign object
lucid_june_weight_data <- svydesign(id = ~1, weights=~weight, data = lucid_june_final)


#####################
# 2019 ACS 5-year estimates sex, age, education, race 
#####################
pop.types_female <- data.frame(female = c(0, 1), Freq = unlist(c(nrow(lucid_june_final)*.492, nrow(lucid_june_final)*.508)))

#No HS, HS grad, Some College, 2-Year College, 4-Year, PostGrad
pop.types_educ<- data.frame(educ = c(1:6), Freq = unlist(c(nrow(lucid_june_final)*.120, nrow(lucid_june_final)*.270, nrow(lucid_june_final)*.204, nrow(lucid_june_final)*.085, nrow(lucid_june_final)*.198, nrow(lucid_june_final)*.124)))

#white, non-hispanic 
pop.types_white <- data.frame(white = c(0,1), Freq = unlist(c(nrow(lucid_june_final)*.393, nrow(lucid_june_final)*.607)))

#age 
pop.types_agegroup<-data.frame(agegroup=c(0:5), Freq = unlist(c(nrow(lucid_june_final)*0.214, nrow(lucid_june_final)*0.171, nrow(lucid_june_final)*0.162, nrow(lucid_june_final)*0.171, nrow(lucid_june_final)*0.147, nrow(lucid_june_final)*0.135)))

####################
# Rake the Weights #
####################
lucid_june_weight_data <- rake(lucid_june_weight_data, list(~female, ~educ, ~white, ~agegroup), list(pop.types_female, pop.types_educ, pop.types_white, pop.types_agegroup))

lucid_june_final$weight <- 1/lucid_june_weight_data$prob
hist(lucid_june_final$weight); mean(lucid_june_final$weight)

summary(lucid_june_final$weight)

#Trim weights
lucid_june_weight_data_trim <- trimWeights(lucid_june_weight_data, upper=5,
                                           strict=TRUE)

lucid_june_final$weight <- 1/lucid_june_weight_data_trim$prob
hist(lucid_june_final$weight); mean(lucid_june_final$weight)

summary(lucid_june_final$weight)

write.csv(lucid_june_final, file="lucid_june_weight.csv", row.names = F)

################################
# Confirm Survey Weight Results 
################################

# Female #
crosstab(lucid_june_final$female, rep(1, nrow(lucid_june_final)), weight = lucid_june_final$weight, plot=F, prop.r=T)
CrossTable(lucid_june_final$female)

# Education #
crosstab(lucid_june_final$educ, rep(1, nrow(lucid_june_final)), weight = lucid_june_final$weight, plot=F, prop.r=T)
CrossTable(lucid_june_final$educ)

# White#
crosstab(lucid_june_final$white, rep(1, nrow(lucid_june_final)), weight = lucid_june_final$weight, plot=F, prop.r=T)
CrossTable(lucid_june_final$white)

# Age#
crosstab(lucid_june_final$agegroup, rep(1, nrow(lucid_june_final)), weight = lucid_june_final$weight, plot=F, prop.r=T)
CrossTable(lucid_june_final$agegroup)



############
#Table C10 #
############
ssi_wdata <- svydesign(~0, weights=ssi_final$weight, data=ssi_final)

summary(ban_ssi2_weight <-
  svyglm(ban ~ socialmedia +  republican +  independent + female + agegroup + 
           white+ income+ educ + localTV +cableTV + radio+ 
           newspaper_online_print, ssi_wdata))

summary(patrol_ssi2_weight <-
  svyglm(patrol ~ socialmedia +  republican +  independent + female + agegroup + 
           white+ income+ educ + localTV +cableTV + radio+ 
           newspaper_online_print, ssi_wdata))


tab_labels <- c( "Social Media", "Republican", "Independent", "Female",  
                 "Age",  "White", "Income", "Education", "Broadcast TV", 
                 "Cable TV", "Radio", "Newspaper")

stargazer(ban_ssi2_weight, patrol_ssi2_weight, style="ajps", out.header=T,
           model.numbers = TRUE,
         covariate.labels = tab_labels,
          dep.var.labels =  c("Support Ban", "Support Patrol"),
          out = "table_ssi_weight.tex", 
          label = "table_ssi_weight",
          font.size = "footnotesize",
         column.sep.width = "2pt",
         omit.stat = c("ser","f")
          )



############
#Table C11 #
############
lucid_march_wdata <- svydesign(~0, weights=lucid_march_final$weight, data=lucid_march_final)

summary(ban_lucid_march_weight <-
  svyglm(ban ~ socialmedia + republican +  independent + ideology_7pt+ female + 
           agegroup + white+ income+ educ + fav_MuslimAmericans+ broadcast_tv+ 
           info_CNN + info_FOX + info_MSNBC + info_radio+ info_localnewspaper+ 
           info_NYT, lucid_march_wdata))

summary(patrol_lucid_march_weight  <-
  svyglm(patrol ~ socialmedia + republican +  independent + ideology_7pt+ female + 
           agegroup + white+ income+ educ + fav_MuslimAmericans+ broadcast_tv+ 
           info_CNN + info_FOX + info_MSNBC + info_radio+ info_localnewspaper+ 
           info_NYT, lucid_march_wdata))

#Table
tab_labels <- c( "Social Media", "Republican", "Independent", "Ideology (Lib-Con)", 
                 "Female", "Age",  "White", "Income", "Education", 
                 "Muslim Favorability", "Broadcast TV", "CNN", "FOX", "MSNBC", 
                 "Radio", "Local Newspaper", "NYT")

stargazer(ban_lucid_march_weight, patrol_lucid_march_weight, style="ajps", out.header=T,
           model.numbers = TRUE,
         covariate.labels = tab_labels,
          dep.var.labels =  c("Support Ban", "Support Patrol"),
          out = "table_lucid_march_weight.tex", 
          label = "table_lucid_march_weight",
          font.size = "footnotesize",
         column.sep.width = "2pt",
         omit.stat = c("ser","f")
          )


############
#Table C12 #
############
lucid_june_wdata <- svydesign(~0, weights=lucid_june_final$weight, data=lucid_june_final)

summary(ban_lucid_june_weight <-
  svyglm(ban ~ socialmedia + republican +  independent + ideology_7pt+ female + 
           agegroup + white+ income+ educ + fav_MuslimAmericans+ broadcast_tv+ 
           info_CNN + info_FOX + info_MSNBC + info_radio_npr+ info_radio_hannity+ 
           info_localnewspaper+ info_NYT +info_wsj, lucid_june_wdata))

summary(patrol_lucid_june_weight  <-
  svyglm(patrol ~ socialmedia + republican +  independent + ideology_7pt+ female + 
           agegroup + white+ income+ educ + fav_MuslimAmericans+ broadcast_tv+ 
           info_CNN + info_FOX + info_MSNBC + info_radio_npr+ info_radio_hannity+ 
           info_localnewspaper+ info_NYT +info_wsj, lucid_june_wdata))

summary(mosque_lucid_june_weight  <-
  svyglm(restrict_mosques ~ socialmedia + republican +  independent + ideology_7pt+ 
           female + agegroup + white+ income+ educ + fav_MuslimAmericans+ 
           broadcast_tv+ info_CNN + info_FOX + info_MSNBC + info_radio_npr+ 
           info_radio_hannity+ info_localnewspaper+ info_NYT +info_wsj, 
         lucid_june_wdata))

summary(weapon_lucid_june_weight  <-
  svyglm(noweapon_muslims ~ socialmedia + republican +  independent + 
           ideology_7pt+ female + agegroup + white+ income+ educ + 
           fav_MuslimAmericans+ broadcast_tv+ info_CNN + info_FOX + 
           info_MSNBC + info_radio_npr+ info_radio_hannity+ info_localnewspaper+
           info_NYT +info_wsj, lucid_june_wdata))

#Table
tab_labels <- c( "Social Media", "Republican", "Independent", "Ideology (Lib-Con)", 
                 "Female", "Age",  "White", "Income", "Education", 
                 "Muslim Favorability", "Broadcast TV", "CNN", "FOX", "MSNBC", 
                 "Radio NPR","Radio Hannity",  "Local Newspaper", "NYT", "WSJ")

stargazer(ban_lucid_june_weight, patrol_lucid_june_weight, 
          mosque_lucid_june_weight, weapon_lucid_june_weight, 
          style="ajps", out.header=T,
           model.numbers = TRUE,
         covariate.labels = tab_labels,
          dep.var.labels =  c("Support Ban", "Support Patrol", "Restrict Mosques", "Weapon Ban"),
          out = "table_lucid_june_weight.tex", 
          label = "table_lucid_june_weight",
          font.size = "footnotesize",
         column.sep.width = "2pt",
         omit.stat = c("ser","f")
          )


###################
#Tables C7 and C8 #
###################
#EPA Lucid March
summary(epa_lucid_march_1 <-
  lm(epa ~ socialmedia + republican +  independent + ideology_7pt+ female + 
       age + white+ income+ educ, data = lucid_march_final))

summary(epa_lucid_march_2 <-
  lm(epa ~ socialmedia + republican +  independent + ideology_7pt+ female + 
       age + white+ income+ educ + broadcast_tv+ info_CNN + info_FOX + 
       info_MSNBC + info_radio+ info_localnewspaper+ info_NYT, 
     data = lucid_march_final))

summary(epa_lucid_march_3 <-
  lm(epa ~ info_Twitter + info_Facebook + republican +  independent + 
       ideology_7pt+ female + age + white+ income+ educ + broadcast_tv+ 
       info_CNN + info_FOX + info_MSNBC + info_radio+ info_localnewspaper+ 
       info_NYT, data = lucid_march_final))


#Assisted Suicide Lucid March
summary(assisted_suicide_lucid_march_1 <-
  lm(assisted_suicide ~ socialmedia + republican +  independent + ideology_7pt+ 
       female + age + white+ income+ educ, data = lucid_march_final))

summary(assisted_suicide_lucid_march_2 <-
  lm(assisted_suicide ~ socialmedia + republican +  independent + ideology_7pt+ 
       female + age + white+ income+ educ + broadcast_tv+ info_CNN + info_FOX + 
       info_MSNBC + info_radio+ info_localnewspaper+ info_NYT, 
     data = lucid_march_final))

summary(assisted_suicide_lucid_march_3 <-
  lm(assisted_suicide ~ info_Twitter + info_Facebook + republican + 
       independent + ideology_7pt+ female + age + white+ income+ educ + 
       broadcast_tv+ info_CNN + info_FOX + info_MSNBC + info_radio+ 
       info_localnewspaper+ info_NYT, data = lucid_march_final))


#Tables 
tab_labels <- c( "Social Media", "Republican", "Independent", "Ideology (Lib-Con)", 
                 "Female", "Age",  "White", "Income", "Education", 
                 "Broadcast TV", "CNN", "FOX", "MSNBC", "Radio NPR",
                 "Radio Hannity",  "Local Newspaper", "NYT", "WSJ")

tab_labels2 <- c( "Twitter",  "Facebook","Republican", "Independent", 
                  "Ideology (Lib-Con)", "Female", "Age",  "White", 
                  "Income", "Education", "Broadcast TV", "CNN", "FOX", 
                  "MSNBC", "Radio NPR","Radio Hannity",  "Local Newspaper", 
                  "NYT", "WSJ")

stargazer(epa_lucid_march_1, epa_lucid_march_2, assisted_suicide_lucid_march_1, 
          assisted_suicide_lucid_march_2, 
          style="ajps", out.header=T,
           model.numbers = TRUE,
         covariate.labels = tab_labels,
          dep.var.labels =  c("EPA", "Assisted Suicide"),
          out = "table_lucid_march_placebo.tex", 
          label = "table_lucid_march_placebo",
          font.size = "footnotesize",
         column.sep.width = "2pt",
         omit.stat = c("ser","f")
          )

stargazer(epa_lucid_march_3, assisted_suicide_lucid_march_3, 
          style="ajps", out.header=T,
           model.numbers = TRUE,
         covariate.labels = tab_labels2,
          dep.var.labels =  c("EPA", "Assisted Suicide"),
          out = "table_lucid_march_placebo2.tex", 
          label = "table_lucid_march_placebo2",
          font.size = "footnotesize",
         column.sep.width = "2pt",
         omit.stat = c("ser","f")
          )


##################
#Table C1 and C2 #
##################

#Variable Creation
#Measure of preferring social media over all other news sources

#function to transform media variables to range from 0-1
range01 <- function(x){(x-min(x))/(max(x)-min(x))}

#Lucid June Data 
#All media sources
lucid_june_final$allmedia<-(lucid_june_final$info_CNN+lucid_june_final$info_FOX+
                              lucid_june_final$info_MSNBC + 
                              lucid_june_final$info_radio_npr + 
                              lucid_june_final$broadcast_tv+
                              lucid_june_final$info_radio_hannity+
                              lucid_june_final$info_localnewspaper +
                              lucid_june_final$info_NYT+
                              lucid_june_final$info_wsj)/9  

summary(lucid_june_final$allmedia)

##Transform 0-1
lucid_june_final$allmedia<-range01(lucid_june_final$allmedia)
summary(lucid_june_final$allmedia)

#Second variable that exclude Fox, MSNBC, and Hannity 
lucid_june_final$traditionalmedia<-(lucid_june_final$broadcast_tv+
                                      lucid_june_final$info_localnewspaper+
                                      lucid_june_final$info_NYT+
                                      lucid_june_final$info_wsj+
                                      lucid_june_final$info_radio_npr+
                                      lucid_june_final$info_CNN)/6  

summary(lucid_june_final$traditionalmedia)

##Transform 0-1
lucid_june_final$traditionalmedia<-range01(lucid_june_final$traditionalmedia)
summary(lucid_june_final$traditionalmedia)

##Transform Soc Media 0-1
lucid_june_final$socialmedia2<-range01(lucid_june_final$socialmedia)
summary(lucid_june_final$socialmedia2)

#Create the two preference measures
lucid_june_final$socialmedia_ratio1<-lucid_june_final$socialmedia2 - lucid_june_final$allmedia
summary(lucid_june_final$socialmedia_ratio1)

lucid_june_final$socialmedia_ratio2<-lucid_june_final$socialmedia2 - lucid_june_final$traditionalmedia
summary(lucid_june_final$socialmedia_ratio2)

#Models
#Soc Media > All media
summary(ban_lucid_june_ratio1 <-
          lm(ban ~ socialmedia_ratio1 + republican +  independent + ideology_7pt+ 
               female + age + white+ income+ educ + fav_MuslimAmericans, 
             data = lucid_june_final))

summary(patrol_lucid_june_ratio1 <-
          lm(patrol ~ socialmedia_ratio1 + republican +  independent + 
               ideology_7pt+ female + age + white+ income+ educ + 
               fav_MuslimAmericans, data = lucid_june_final))

summary(mosque_lucid_june_ratio1 <-
          lm(restrict_mosques ~ socialmedia_ratio1 + republican +  independent+
               ideology_7pt+ female + age + white+ income+ educ + 
               fav_MuslimAmericans, data = lucid_june_final))

summary(noweapon_lucid_june_ratio1 <-
          lm(noweapon_muslims ~ socialmedia_ratio1 + republican + independent+ 
               ideology_7pt+ female + age + white+ income+ educ + 
               fav_MuslimAmericans, data = lucid_june_final))

#Soc Media > All media (excluding Fox, MSNBC, and Hannity)
summary(ban_lucid_june_ratio2 <-
          lm(ban ~ socialmedia_ratio2 + republican +  independent + 
               ideology_7pt+ female + age + white+ income+ educ + 
               fav_MuslimAmericans, data = lucid_june_final))

summary(patrol_lucid_june_ratio2 <-
          lm(patrol ~ socialmedia_ratio2 + republican + independent +
               ideology_7pt+ female + age + white+ income+ educ + 
               fav_MuslimAmericans, data = lucid_june_final))

summary(mosque_lucid_june_ratio2 <-
          lm(restrict_mosques ~ socialmedia_ratio2 + republican + independent +
               ideology_7pt+ female + age + white+ income+ educ +
               fav_MuslimAmericans, data = lucid_june_final))

summary(noweapon_lucid_june_ratio2 <-
          lm(noweapon_muslims ~ socialmedia_ratio2 + republican +  independent+
               ideology_7pt+ female + age + white+ income+ educ + 
               fav_MuslimAmericans, data = lucid_june_final))

#Tables
tab_labels <- c( "Prioritize Soc. Media$>$Other Media", "Republican", 
                 "Independent", "Ideology (Lib-Con)", "Female", "Age",
                 "White", "Income", "Education", "Muslim Favorability")

stargazer(ban_lucid_june_ratio1, patrol_lucid_june_ratio1, 
          mosque_lucid_june_ratio1,noweapon_lucid_june_ratio1, 
          style="ajps", out.header=T,
           model.numbers = TRUE,
         covariate.labels = tab_labels,
          dep.var.labels =  c("Support Ban", "Support Patrol", 
                              "Restrict Mosques", "Weapon Ban"),
          out = "table_lucid_june_alternative_measure.tex", 
          label = "table_lucid_june_alternative_measure",
          font.size = "footnotesize",
         column.sep.width = "2pt",
         omit.stat = c("ser","f")
          )

stargazer(ban_lucid_june_ratio2, patrol_lucid_june_ratio2, 
          mosque_lucid_june_ratio2,noweapon_lucid_june_ratio2, 
          style="ajps", out.header=T,
           model.numbers = TRUE,
         covariate.labels = tab_labels,
          dep.var.labels =  c("Support Ban", "Support Patrol", 
                              "Restrict Mosques", "Weapon Ban"),
          out = "table_lucid_june_alternative_measure2.tex", 
          label = "table_lucid_june_alternative_measure2",
          font.size = "footnotesize",
         column.sep.width = "2pt",
         omit.stat = c("ser","f"))



###########
#Table B5 #
###########
#Ban DV:
summary(ban_nation_na_1_weight <-
  lm(ban2 ~ socialmedia+ female + age + white+ income+ education + republican +  
       independent+ ideo5, data = nation_final, weights=weight))

summary(ban_nation_na_2_weight <-
  lm(ban2 ~ socialmedia+ female + age + white+ income+ education + republican +  
       independent+ ideo5+ info_FOX+info_CNN+info_MSNBC+broadcast_tv+
       info_radio_npr+info_localnewspaper+ info_NYT, 
     data = nation_final, weights=weight))

summary(ban_nation_1_weight <-
  lm(ban ~ socialmedia+ female + age + white+ income+ education + republican +  
       independent+ ideo5, data = nation_final, weights=weight))

summary(ban_nation_2_weight <-
  lm(ban ~ socialmedia+ female + age + white+ income+ education + republican +  
       independent+ ideo5+ info_FOX+info_CNN+info_MSNBC+broadcast_tv+
       info_radio_npr+info_localnewspaper+ info_NYT, 
     data = nation_final, weights=weight))


tab_labels <- c( "Social Media","Female", "Age",  "White", "Income", 
                 "Education", "Republican", "Independent", "Ideology (Lib-Con)", 
                 "FOX", "CNN", "MSNBC", "Broadcast TV", "Radio: NPR", 
                 "Local Newspaper", "NYT")

stargazer(ban_nation_na_1_weight, ban_nation_na_2_weight, 
          ban_nation_1_weight,ban_nation_2_weight, 
          style="ajps", out.header=T,
           model.numbers = TRUE,
         covariate.labels = tab_labels,
          dep.var.labels =  c("`Not Sure' Excluded", "`Not Sure' Included"),
          out = "table_nationscape_main_weighted.tex", 
          label = "table_nationscape_main_weighted",
          font.size = "footnotesize",
         column.sep.width = "2pt",
         omit.stat = c("ser","f")
          )

###########
#Table C9 #
###########
#Wall DV
summary(wall_nation_1_weight <-
  lm(wall ~ socialmedia+ female + age + white+ income+ education + republican +  
       independent+ ideo5, data = nation_final, weights=weight))

summary(wall_nation_2_weight <-
  lm(wall ~ socialmedia+ female + age + white+ income+ education + republican +  
       independent+ ideo5+ info_FOX+info_CNN+info_MSNBC+broadcast_tv+
       info_radio_npr+info_localnewspaper+ info_NYT, 
     data = nation_final, weights=weight))

tab_labels <- c( "Social Media","Female", "Age",  "White", "Income", 
                 "Education", "Republican", "Independent", "Ideology (Lib-Con)", 
                 "FOX", "CNN", "MSNBC", "Broadcast TV", "Radio: NPR", 
                 "Local Newspaper", "NYT")

stargazer(wall_nation_1_weight, wall_nation_2_weight, 
          style="ajps", out.header=T,
           model.numbers = TRUE,
         covariate.labels = tab_labels,
          dep.var.labels =  c(),
          out = "table_nationscape_wall_weighted.tex", 
          label = "table_nationscape_wall_weighted",
          font.size = "footnotesize",
         column.sep.width = "2pt",
         omit.stat = c("ser","f")
          )

###########
#Table B9 #
###########
#PID
summary(ban_nation_na_2_weight_dem <-
  lm(ban2 ~ socialmedia+ female + age + white+ income+ education + 
        ideo5+ info_FOX+info_CNN+info_MSNBC+broadcast_tv+info_radio_npr+
       info_localnewspaper+ info_NYT, data = nation_final, 
     weights=weight, subset=nation_final$democrat==1))

summary(ban_nation_2_weight_dem <-
  lm(ban ~ socialmedia+ female + age + white+ income+ education + 
        ideo5+ info_FOX+info_CNN+info_MSNBC+broadcast_tv+
       info_radio_npr+info_localnewspaper+ info_NYT, data = nation_final, 
     weights=weight, subset=nation_final$democrat==1))

summary(ban_nation_na_2_weight_rep <-
  lm(ban2 ~ socialmedia+ female + age + white+ income+ education + 
        ideo5+ info_FOX+info_CNN+info_MSNBC+broadcast_tv+info_radio_npr+
       info_localnewspaper+ info_NYT, data = nation_final, 
     weights=weight, subset=nation_final$republican==1))

summary(ban_nation_2_weight_rep <-
  lm(ban ~ socialmedia+ female + age + white+ income+ education + 
        ideo5+ info_FOX+info_CNN+info_MSNBC+broadcast_tv+
       info_radio_npr+info_localnewspaper+ info_NYT, data = nation_final, 
     weights=weight, subset=nation_final$republican==1))


tab_labels <- c( "Social Media","Female", "Age",  "White", "Income", 
                 "Education", "Ideology (Lib-Con)", "FOX", "CNN", "MSNBC", 
                 "Broadcast TV", "Radio: NPR", "Local Newspaper", "NYT")

stargazer(ban_nation_na_2_weight_dem, ban_nation_2_weight_dem, 
          ban_nation_na_2_weight_rep, ban_nation_2_weight_rep, 
          style="ajps", out.header=T,
           model.numbers = TRUE,
         covariate.labels = tab_labels,
          dep.var.labels =  c("`Not Sure' Excluded","`Not Sure' Excluded", 
                              "`Not Sure' Included", "`Not Sure' Included"),
          out = "table_nationscape_partyid.tex", 
          label = "table_nationscape_partyid",
          font.size = "footnotesize",
         column.sep.width = "2pt",
         omit.stat = c("ser","f")
          )


###########
#Table C3 #
###########
#Measure of preferring Social Media over all other news sources

#All media sources
nation_final$allmedia<-(nation_final$info_FOX+ nation_final$info_CNN+ 
                          nation_final$info_MSNBC+ nation_final$broadcast_tv+ 
                          nation_final$info_radio_npr+ 
                          nation_final$info_localnewspaper+ 
                          nation_final$info_NYT)/7  

summary(nation_final$allmedia)

#Ratio measure
nation_final$socialmedia_ratio<-nation_final$socialmedia - nation_final$allmedia
summary(nation_final$socialmedia_ratio)

#Models 
summary(ban_nation_na_ratio <-
  lm(ban2 ~ socialmedia_ratio+ female + age + white+ income+ education + republican +  
       independent+ ideo5, data = nation_final, weights=weight))

summary(ban_nation_ratio <-
  lm(ban ~ socialmedia_ratio+ female + age + white+ income+ education + republican +  
       independent+ ideo5, data = nation_final, weights=weight))

#Table
tab_labels <- c( "Prioritize Soc. Media$>$Other Media","Female", "Age",  
                 "White", "Income", "Education", "Republican", 
                 "Independent", "Ideology (Lib-Con)")

stargazer(ban_nation_na_ratio, ban_nation_ratio, style="ajps", out.header=T,
           model.numbers = TRUE,
         covariate.labels = tab_labels,
          dep.var.labels =  c("`Not Sure' Excluded", "`Not Sure' Included"),
          out = "table_nationscape_alternative_measure.tex", 
          label = "table_nationscape_alternative_measure",
          font.size = "footnotesize",
         column.sep.width = "2pt",
         omit.stat = c("ser","f")
          )


###########
#Figure 7 #
###########

#Margins
margin_nationscape_na <- as.data.frame(summary(margins(ban_nation_na_2_weight,
                         variables=c("socialmedia", "info_FOX", "info_CNN", 
                                     "info_MSNBC", "broadcast_tv", 
                                     "info_radio_npr", "info_localnewspaper",  
                                     "info_NYT"), change = "minmax"))) %>% 
                         select(c("AME","lower","upper", "factor"))



#Plot
# IVs
var<-rep(c("Broadcast TV","CNN","FOX","Local Newspaper","MSNBC",
           "NYT","Radio:NPR","Social Media"), times=1) 

# DVs 
dep_var<-rep(c("1"), each=8)

df_nationscape<-cbind(margin_nationscape_na,var,dep_var)
#labels
lab <- c("1" = "Muslim Ban") 

ggplot(data = df_nationscape, aes(x = var%>%fct_relevel(c("Social Media",
                                    "FOX", "CNN", "MSNBC",  "Broadcast TV", 
                                    "Radio:NPR", "Local Newspaper", "NYT"))%>%fct_rev, 
                                        y = AME,ymin=lower, ymax = upper))+
  facet_grid(.~dep_var, labeller = as_labeller(lab))+
  geom_pointrange(size=.75,position=position_dodge(width=.5))+
  theme_bw(base_size=20,base_family="Times")+
  coord_flip()+
  scale_y_continuous(limits=c(-.11,.11), breaks=seq( -.1, .1, .05,))+
  theme(panel.grid.major.x = element_blank(),panel.grid.major.y = element_blank())+
  theme(strip.background = element_rect(fill="white"))+
  theme(strip.text.x = element_text(size = 20))+
  theme(legend.title=element_blank(),
        legend.key.size=unit(2.0, 'lines'))+
	geom_hline(yintercept=0, color="maroon", size=.5, linetype = 2) +
  theme(plot.margin=unit(c(1,2,1,1),"cm"))+
  labs(y="Change in Predicted Value of Support (Min-Max)",x=NULL,title=NULL)
  ggsave("Figure 7.png", width = 8, height = 8)



###########
#Table C4 #
###########
  
#Calculate Muslim Fav. +1 SD above mean 
mean(lucid_june_final$fav_MuslimAmericans)+sd(lucid_june_final$fav_MuslimAmericans)

#Filter Data according to value above
lucid_june_selection_effect<-lucid_june%>% 
    drop_na() %>% 
    filter(fav_MuslimAmericans>=8.69)  %>% 
    dplyr::select(ban, patrol, restrict_mosques, noweapon_muslims, 
                  socialmedia, info_Twitter, info_Facebook, broadcast_tv,
                  info_CNN, info_FOX, info_MSNBC, info_radio_npr, 
                  info_radio_hannity, info_localnewspaper, info_NYT, info_wsj, 
                  democrat, republican, independent, ideology_7pt, female, 
                  white, age, income, educ) %>% data.frame()

##Models

#Ban Lucid June
summary(ban_lucid_june_se <-
  lm(ban ~ socialmedia + republican +  independent + ideology_7pt+ female + age+ 
       white+ income+ educ, data = lucid_june_selection_effect))

summary(ban_lucid_june_se2 <-
  lm(ban ~ socialmedia + republican +  independent + ideology_7pt+ female + age+ 
       white+ income+ educ + broadcast_tv+ info_CNN + info_FOX + info_MSNBC + 
       info_radio_npr+ info_radio_hannity + info_localnewspaper+ info_NYT + 
       info_wsj, data = lucid_june_selection_effect))


#Patrol Lucid June
summary(patrol_lucid_june_se <-
  lm(patrol ~ socialmedia + republican +  independent + ideology_7pt+ female + 
       age + white+ income+ educ, data = lucid_june_selection_effect))

summary(patrol_lucid_june_se2 <-
  lm(patrol ~ socialmedia + republican +  independent + ideology_7pt+ female + 
       age + white+ income+ educ + broadcast_tv+ info_CNN + info_FOX + 
       info_MSNBC + info_radio_npr+ info_radio_hannity + info_localnewspaper+ 
       info_NYT + info_wsj, data = lucid_june_selection_effect))


#Restrict Mosques Lucid June
summary(restrict_mosques_lucid_june_se <-
  lm(restrict_mosques ~ socialmedia + republican +  independent + ideology_7pt+
       female + age + white+ income+ educ, data = lucid_june_selection_effect))

summary(restrict_mosques_lucid_june_se2 <-
  lm(restrict_mosques ~ socialmedia + republican +  independent + ideology_7pt+
       female + age + white+ income+ educ + broadcast_tv+ info_CNN + info_FOX +
       info_MSNBC + info_radio_npr+ info_radio_hannity + info_localnewspaper+
       info_NYT + info_wsj, data = lucid_june_selection_effect))

#Weapon Ban Lucid June
summary(noweapon_muslims_lucid_june_se <-
  lm(noweapon_muslims ~ socialmedia + republican +  independent + ideology_7pt+
       female + age + white+ income+ educ, data = lucid_june_selection_effect))

summary(noweapon_muslims_lucid_june_se2 <-
  lm(noweapon_muslims ~ socialmedia + republican +  independent + ideology_7pt+
       female + age + white+ income+ educ + broadcast_tv+ info_CNN + info_FOX +
       info_MSNBC + info_radio_npr+ info_radio_hannity + info_localnewspaper+
       info_NYT + info_wsj, data = lucid_june_selection_effect))

#Table
tab_labels <- c( "Social Media", "Republican", "Independent", "Ideology (Lib-Con)", 
                 "Female",  "Age",  "White", "Income", "Education", "Broadcast TV", 
                 "CNN", "FOX", "MSNBC", "Radio NPR", "Radio Hannity", 
                 "Local Newspaper", "NYT", "WSJ")

stargazer(ban_lucid_june_se, ban_lucid_june_se2, patrol_lucid_june_se, 
          patrol_lucid_june_se2, restrict_mosques_lucid_june_se, 
          restrict_mosques_lucid_june_se2, noweapon_muslims_lucid_june_se, 
          noweapon_muslims_lucid_june_se2, 
          style="ajps", out.header=T,
           model.numbers = TRUE,
         covariate.labels = tab_labels,
          dep.var.labels =  c("Support Ban", "Support Patrol", 
                              "Restrict Mosques", "Weapon Ban"),
          out = "table_lucid_june_main.tex", 
          label = "table_lucid_june_main",
          font.size = "footnotesize",
         column.sep.width = "2pt",
         omit.stat = c("ser","f")
          )


#The End



































